app.controller('six', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //修改案例状态
    $scope.changeStatus = function () {
        //alert(0);
        if(!$scope.caseId){
            return;
        }
        var remark = $(".remark").val();
        if(remark.length > 200){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注字数不能超过200个字"));
            return;
        }
        if(remark.length == 0 && $scope.imageUrls.length == 0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注或拍照不能为空"));
            return;
        }
        /*if($scope.imageUrls.length!=0 && $scope.imageUrls.length > 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传9张图片"));
            return;
        }*/

        var postData = {
            "caseId": $scope.caseId,
            "status": $scope.status,
            "optionItem": $scope.item.optionItem,
            "urls": JSON.stringify($scope.imageUrls)
        }
        postData.remark = $(".remark").val();

        //alert(JSON.stringify(postData));
        postHttp($http, 'installtionCase/updateCaseStatus', postData, function (res) {
            if (res.code === 0 && res.msg == "success") {
                // $rootScope.tipPopups.show(($scope.tipMessage = '成功'));
                $state.go(one.stateName);
            } else {
                $rootScope.tipPopups.show(($scope.tipMessage = res.msg));
            }
        });
    }


    //拍照
    $scope.takePhoto = function () {
        /*if($scope.imageUrls.length >= 9){
            $rootScope.tipPopups.show(($scope.tipMessage = "最多上传9张图片"));
            return;
        }*/

        var scanType = "sdm1UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
             //context.uploadImage(getToken(),scanType);
			 window.webkit.messageHandlers.uploadImage.postMessage({"scanType":scanType});


//            var token = getToken();
//            var reqeustData = {};
//            reqeustData.username = "support@jbkj.net.cn";
//            reqeustData.username = "13691179771@163.com";
//            reqeustData.password = "123456";
//
//            var url = "http://183.195.106.118:8150/fec-api/api/login";
//            // var url = "https://cnmpse2e-itg.ext.hp.com/fec-api/api/login";
//            postHttp2($http, url, reqeustData, function (res) {
//                if (res.msg === 'success') {
//                    token = res.token;
//                    context.uploadImage(token,scanType);
//                }
//            });
        }
    }

    //存入数组图片路径
    $scope.photoResult = function (url) {
        if (navigator.onLine){ //正常工作
            if(typeof(url)=='string'){
                $scope.imageUrls.push(JSON.parse(url).imageurl);
            }else{
                $scope.imageUrls.push(url.imageurl);
            }
            $scope.$applyAsync();
        }else { //执行离线状态时的任务
            $rootScope.tipPopups.show(($scope.tipMessage = "当前网络不佳，稍后再试"));
            return;
        }
    }

    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }


    //录入基本信息
	$scope.fourteen = function(){
	    if(1==$scope.c){  //选中 “是”
            $state.go(fourteen.stateName);
        }else{
            $state.go(az2.stateName);
        }
	}
	

    //初始化
    function init(){
        //案例id
        $scope.caseId = eight.params.caseId;
        if(!eight.params.caseId){
            $scope.caseId = localStorage.getItem("sdm-caseId");
        }
        $scope.status = 2;
        $scope.item = {
            optionItem:'出墨'
        };

        $scope.imageUrls = [];
    }
    
   init();

    reloadJs();
});

//返回扫描结果
function sdm1UploadImageResult(msg) {
    var $scope = getScope("six");
    $scope.photoResult(msg);
}